package com.tarena.lbs.basic.dao.repository.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageInfo;
import com.tarena.lbs.base.protocol.exception.BusinessException;
import com.tarena.lbs.base.protocol.pager.PageResult;
import com.tarena.lbs.basic.dao.mapper.TagMapper;
import com.tarena.lbs.basic.dao.repository.TagRepository;
import com.tarena.lbs.basic.pojo.po.TagLibraryPO;
import com.tarena.lbs.basic.pojo.query.TagQuery;
import com.tarena.lbs.basic.pojo.vo.TagLibraryVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;

@Repository
@Slf4j
public class TagRepositoryImpl implements TagRepository {
    @Autowired
    private TagMapper tagMapper;

    @Override
    public List<TagLibraryPO> getTagsBytType(Integer tagType) {
        // 默认只查询启用状态的标签
        QueryWrapper<TagLibraryPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("tag_type", tagType)
                   .eq("status", 0)  // 默认只返回启用状态的标签
                   .eq("deleted", 0); // 排除已删除的标签
        return tagMapper.selectList(queryWrapper);
    }

    @Override
    public PageInfo<TagLibraryPO> getAllTags(TagQuery query) {
        log.info("查询标签列表，参数：{}", query);
        QueryWrapper<TagLibraryPO> queryWrapper = assembleTagQuery(query);
        // 只保留未删除条件，移除强制的status=0条件
        queryWrapper.eq("deleted", 0);
        List<TagLibraryPO> pos = tagMapper.selectList(queryWrapper);
        return new PageInfo<>(pos);
    }

    /**
     * 组装查询条件
     */
    private QueryWrapper<TagLibraryPO> assembleTagQuery(TagQuery query) {
        QueryWrapper<TagLibraryPO> queryWrapper = new QueryWrapper<>();
        if (query.getTagName() != null) {
            queryWrapper.like("tag_name", query.getTagName());
        }
        if (query.getTagType() != null) {
            queryWrapper.eq("tag_type", query.getTagType());
        }
        if (query.getTagParentId() != null) {
            queryWrapper.eq("tag_parent_id", query.getTagParentId());
        }
        // 如果查询条件中指定了status，则使用查询条件中的status
        if (query.getStatus() != null) {
            queryWrapper.eq("status", query.getStatus());
        }
        // 添加更新时间查询条件
        if (query.getUpdateAt() != null) {
            queryWrapper.ge("update_at", query.getUpdateAt());
        }
        // 添加排序，按照更新时间倒序
        queryWrapper.orderByDesc("update_at");
        log.info("查询标签列表，组装查询条件：{}", queryWrapper);

        return queryWrapper;
    }

    @Override
    public TagLibraryPO getTagById(Integer id) {
        return tagMapper.selectById(id);
    }

    @Override
    public TagLibraryPO getTagByNameAndType(String tagName, Integer tagType) {
        QueryWrapper<TagLibraryPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("tag_name", tagName)
                .eq("tag_type", tagType);
        return tagMapper.selectOne(queryWrapper);
    }

    @Override
    public int addTag(TagLibraryPO tagLibraryPO) {
        return tagMapper.insert(tagLibraryPO);
    }

    @Override
    public int updateTag(TagLibraryPO tagLibraryPO) {
        return tagMapper.updateById(tagLibraryPO);
    }

    @Override
    public void delete(Integer id) throws BusinessException {
        // 假删除：更新deleted字段为1（表示已删除）
        TagLibraryPO tagLibraryPO = new TagLibraryPO();
        tagLibraryPO.setId(id);
        tagLibraryPO.setDeleted(1);
        tagLibraryPO.setUpdateAt(new Date());
        int result = tagMapper.updateById(tagLibraryPO);
        if (result != 1) {
            throw new BusinessException("-1","删除账号失败");
        }
    }

    @Override
    public int deleteTag(Integer id){
        // 假删除：更新deleted字段为1（表示已删除）
        TagLibraryPO tagLibraryPO = new TagLibraryPO();
        tagLibraryPO.setId(id);
        tagLibraryPO.setDeleted(1);
        tagLibraryPO.setUpdateAt(new Date());
        return tagMapper.updateById(tagLibraryPO);
    }

    @Override
    public PageResult<TagLibraryVO> getTagById(TagQuery query) {
        return null;
    }

    @Override
    public List<TagLibraryPO> getTagsByParentId(Integer parentId) {
        QueryWrapper<TagLibraryPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("tag_parent_id", parentId)
                .eq("deleted", 0); // 排除已删除的标签
        return tagMapper.selectList(queryWrapper);
    }
}